package in.muton.edushare.academics.panels;

import in.muton.edushare.academics.panels.GradePanelUtil.GradeKeyHelper;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.PropertyModel;

public class EntityGradeEdit extends EdusharePanel {

	private EntityGradePanel parentPanel;

	public EntityGradeEdit(String id, EntityGradePanel panel,
			EduShareBasePage page) {
		super(id, page);
		this.parentPanel = panel;

		Form<Void> f = new Form<Void>("form") {
			@Override
			public void onSubmit() {
				setError(GradePanelUtil.saveTuples(parentPanel.getTupleList()));
				parentPanel.setInitialized(false);
				parentPanel.setActiveMode(EntityGradePanel.VIEW_MODE);
			}

			private static final long serialVersionUID = 6461517620210075959L;
		};
		add(f);

		f.add(new ListView<GradeKeyHelper>("gradeList", parentPanel.getTupleList()) {
			@Override
			protected void populateItem(ListItem<GradeKeyHelper> arg0) {
				final GradeKeyHelper t = arg0.getModelObject();
				arg0.add(new Label("gradeLetter", t.gradeName));
				arg0.add(new Label("gpaValue", t.gpaValue + ""));
				arg0.add(new Label("failGrade", t.passGrade?"":"F"));
				arg0.add(new TextField<Double>("minMarks",
								new PropertyModel<Double>(t, "minMarks"),
								Double.class));
				arg0.add(new TextField<Double>("maxMarks",
								new PropertyModel<Double>(t, "maxMarks"),
								Double.class));
			}

			private static final long serialVersionUID = 8748931346069337850L;
		});
	}

	@Override
	public boolean isVisible() {
		return parentPanel.getActiveMode() == EntityGradePanel.EDIT_MODE;
	}

	private static final long serialVersionUID = -7231516033274029703L;
}
